package com.spyx.activity.controller;


import com.atyx.spyx.model.activity.ActivityInfo;
import com.atyx.spyx.model.activity.ActivityRule;
import com.atyx.spyx.model.product.SkuInfo;
import com.atyx.spyx.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spyx.activity.service.ActivityInfoService;
import com.spyx.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author yhp
 * @since 2024-05-21
 */
@RestController
@RequestMapping("/admin/activity/activityInfo")
//@CrossOrigin
@Api(tags = "活动管理")
public class ActivityInfoController {

    @Autowired
    private ActivityInfoService activityInfoService;

    @ApiOperation("活动分页查询")
    @GetMapping("{page}/{limit}")
    public Result getPageList(@PathVariable("page") Integer page,
                              @PathVariable("limit") Integer limit){

        Page p1 = new Page(page,limit);
        IPage<ActivityInfo> pageModel= activityInfoService.selectPage(p1);
        return Result.ok(pageModel);
    }

    @ApiOperation("主键查询")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable("id") Integer id){
        ActivityInfo activityInfo = activityInfoService.getById(id);
        activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        return Result.ok(activityInfo);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public Result save(@RequestBody ActivityInfo activityInfo){
       // activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        activityInfoService.save(activityInfo);
        return Result.ok();
    }

    @ApiOperation("更新")
    @PutMapping("/update")
    public Result update(@RequestBody ActivityInfo activityInfo){
        activityInfoService.updateById(activityInfo);
        return  Result.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable("id") Long id){
        activityInfoService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        activityInfoService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("根据活动ID,查询活动规则")
    @GetMapping("/findActivityRuleList/{id}")
    public Result findActivityRuleList(@PathVariable("id") Integer activityId){

        Map<String,Object> map= activityInfoService.findActivityRuleListByActivityId(activityId);
        return Result.ok(map);
    }

    //保存规则信息
    @ApiOperation("保存规则信息")
    @PostMapping("saveActivityRule")
    public Result saveRule(@RequestBody ActivityRuleVo activityRuleVo){

        activityInfoService.saveRule(activityRuleVo);
        return Result.ok();
    }


    @ApiOperation("添加活动范围时，根据关键字,查询商品列表")
    @GetMapping("findSkuInfoByKeyword/{keyword}")
    public Result findSkuInfoByKeyword(@PathVariable("keyword")String keyword){
       List<SkuInfo> list=  activityInfoService.getSkuInfoList(keyword);
        return Result.ok(list);
    }

}











